﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.BasicData;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.BasicData;

namespace VOL.SmartMecial.Services.BasicData
{
    /// <summary>
    /// 科室、病区树形结构服务实现
    /// </summary>
    public class TreeService:ITreeService
    {
        /// <summary>
        /// 病区区域仓储
        /// </summary>
        private readonly IBaseRepository<WardArea> _wardAreaRepo;

        /// <summary>
        /// 科室仓储
        /// </summary>
        private readonly IBaseRepository<OfficeModel> _officeModelRepo;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="wardAreaRepo"></param>
        /// <param name="officeModelRepo"></param>
        public TreeService(IBaseRepository<WardArea> wardAreaRepo, IBaseRepository<OfficeModel> officeModelRepo)
        {
            _wardAreaRepo = wardAreaRepo;
            _officeModelRepo = officeModelRepo;
        }

        public async Task<List<TreeDTO>> GetTree()
        {
            // 获取所有科室数据
            var offices = _officeModelRepo.GetAll().ToList();
            
            // 获取所有病区数据
            var wardAreas = _wardAreaRepo.GetAll().ToList();
            
            // 构建树形结构
            List<TreeDTO> rootNodes = new List<TreeDTO>();
            
            // 首先找出顶级科室（没有上级部门或上级部门不存在的科室）
            var topOffices = offices.Where(o => o.ParentDepart == 0 || !offices.Any(p => p.Id == o.ParentDepart)).ToList();
            
            // 构建树
            foreach (var office in topOffices)
            {
                TreeDTO node = new TreeDTO
                {
                    Id = office.Id,
                    TreeName = office.OfficeName,
                    Children = new List<TreeDTO>()
                };
                
                // 递归添加子科室和病区
                BuildOfficeTree(node, office.Id, offices, wardAreas);
                
                rootNodes.Add(node);
            }
            
            return rootNodes;
        }
        
        /// <summary>
        /// 递归构建科室树和病区
        /// </summary>
        /// <param name="parentNode">父节点</param>
        /// <param name="parentOfficeId">父科室ID</param>
        /// <param name="allOffices">所有科室列表</param>
        /// <param name="allWardAreas">所有病区列表</param>
        private void BuildOfficeTree(TreeDTO parentNode, int parentOfficeId, List<OfficeModel> allOffices, List<WardArea> allWardAreas)
        {
            // 添加子科室
            var childOffices = allOffices.Where(o => o.ParentDepart == parentOfficeId).ToList();
            foreach (var childOffice in childOffices)
            {
                TreeDTO childNode = new TreeDTO
                {
                    Id = childOffice.Id,
                    TreeName = childOffice.OfficeName,
                    Children = new List<TreeDTO>()
                };
                
                // 递归添加子科室的子节点
                BuildOfficeTree(childNode, childOffice.Id, allOffices, allWardAreas);
                
                parentNode.Children.Add(childNode);
            }
            
            // 查找当前科室的所有病区
            var currentOffice = allOffices.FirstOrDefault(o => o.Id == parentOfficeId);
            if (currentOffice != null)
            {
                var wards = allWardAreas.Where(w => w.OfficeCode == currentOffice.OfficeCode).ToList();
                
                // 添加病区作为科室的子节点
                foreach (var ward in wards)
                {
                    TreeDTO wardNode = new TreeDTO
                    {
                        Id = ward.Id,
                        TreeName = ward.Name,
                        Children = new List<TreeDTO>() // 病区没有子节点，但仍需初始化列表
                    };
                    
                    parentNode.Children.Add(wardNode);
                }
            }
        }
    }
}
